package com.lemner.motion.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.telecom.Call;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lemner.motion.R;
import com.lemner.motion.base.BaseActivity;
import com.lemner.motion.base.BaseListener;
import com.lemner.motion.base.BaseModel;
import com.lemner.motion.base.URL;
import com.lemner.motion.bean.LoginBean;
import com.lemner.motion.util.FullScreen;
import com.lemner.motion.util.SharedPreferencesUtils;


public class LoginActivity extends BaseActivity {
         private Button login_btn_login;
         private EditText login_et_phone,login_et_pwd;
         private ImageView login_img_back;
         private TextView login_tv_reg,login_tv_forget;
         private String num="";
         private SharedPreferencesUtils util;
         public  static  String studentid="";
         public  static String session="";
    @Override
    public int getLayoutFile() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        FullScreen.fullScreen(this,0x33131313);
        login_btn_login=(Button)findViewById(R.id.login_btn_login);
        login_et_phone=(EditText)findViewById(R.id.login_et_phone);
        login_et_pwd=(EditText)findViewById(R.id.login_et_pwd);
        login_img_back=(ImageView)findViewById(R.id.login_img_back);
        login_tv_reg=(TextView)findViewById(R.id.login_tv_reg);
        login_tv_forget=(TextView)findViewById(R.id.login_tv_forget);
        num=getIntent().getStringExtra("num");
        util=new SharedPreferencesUtils(this);
        if(num!=null&&!num.equals(""))
            login_et_phone.setText(num);
    }

    @Override
    public void initEvent() {
        login_btn_login.setOnClickListener(this);
        login_img_back.setOnClickListener(this);
        login_tv_reg.setOnClickListener(this);
        login_tv_forget.setOnClickListener(this);
    }

    @Override
    public void initData() {
        num=getIntent().getStringExtra("num");
    }

    @Override
    public void onClick(View v) {
            switch (v.getId()) {
                case R.id.login_btn_login:
                        String phone = login_et_phone.getText().toString();
                        String pwd = login_et_pwd.getText().toString();
                        if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(pwd)) {
                            showToast("你输入的值有空,请检查后再试");
                            return;
                        } else {
                            requestLogin(phone, URL.md5(pwd));
                        }
                        break;
                        case R.id.login_img_back:
                            startActivity(new Intent(LoginActivity.this, IndexActivity.class));
                            finish();
                            break;
                        case R.id.login_tv_reg:
                            startActivity(new Intent(LoginActivity.this, Register1Activity.class));
                            finish();
                            break;
                       case R.id.login_tv_forget:
                           //忘记密码逻辑
                            break;
                        default:
                            break;
            }
    }

    BaseListener<LoginBean>listener=new BaseListener<LoginBean>() {
        @Override
        public void onResponse(LoginBean baseBean) {
                 if(baseBean.getReturnCode().equals("0001")){
                     studentid=baseBean.getData().get(0).getStudentId();
                     session=baseBean.getData().get(0).getSessionId();
                     showToast("登陆成功");
                     util.saveString("phone",login_et_phone.getText().toString());
                     util.saveString("pwd",login_et_pwd.getText().toString());
                     startActivity(new Intent(LoginActivity.this, StartRunActivity.class));
                     finish();
                 }
                 else
                     showToast("您的密码有误，请检查后再试");
        }
        @Override
        public void onFail(String msg) {
                     showToast(msg);
        }
    };

    public void requestLogin(String name,String pwd){
        BaseModel<LoginBean> model=new BaseModel<>();
        retrofit2.Call<LoginBean>call=  model.service.getLogin(name, pwd);
        model.callEnqueue(call,listener);
    }

}
